﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace RobotCoder
{
    internal static class clsCommon
    {
        private const string mcstrClassName = "frmDanhSachDuTuyen";         //class name

        /// <summary>
        /// Show Error Message
        /// </summary>
        /// <param name="strModule">Method/function name</param>
        /// <param name="strMsg">Custom message to show</param>
        /// <returns></returns>
        public static bool fncMsgError(string strClass, string strModule, string strMsg)
        {
            bool blnResult = false;

            try
            {
                string strMsgShow = string.Format("Class : {0} \nMethod : {1} \nMessage : \n{2}", strClass, strModule, strMsg);

                //MessageBox.Show(strMsgShow, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                DevExpress.XtraEditors.XtraMessageBox.Show(strMsgShow, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                return true;
            }
            catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show ERROR message
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static bool fncMsgError(string strMsg)
        {
            bool blnResult = false;

            try
            {
                //MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                DevExpress.XtraEditors.XtraMessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                return true;
            }
            catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show information message
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static bool fncMsgInfo(string strMsg)
        {
            bool blnResult = false;

            try
            {
                //MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                DevExpress.XtraEditors.XtraMessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Information);

                return true;
            }
            catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show warning message
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static bool fncMsgWarning(string strMsg)
        {
            bool blnResult = false;

            try
            {
                //MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                DevExpress.XtraEditors.XtraMessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return true;
            }
            catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show confirm message
        /// </summary>
        /// <param name="strMsg"></param>
        /// <returns></returns>
        public static bool fncMsgConfirm(string strMsg)
        {
            bool blnResult = false;

            try
            {
                //if (MessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) return true;
                if (DevExpress.XtraEditors.XtraMessageBox.Show(strMsg, clsConst.gcstrAppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) return true;

                return false;
            }
            catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }

            return blnResult;
        }

        /// <summary>
        /// Show open file dialog
        /// </summary>
        /// <param name="strFilter"></param>
        /// <returns></returns>
        public static string fncOpenFile(string strFilter)
        {
            string strResult = "";
            try
            {
                using (OpenFileDialog objOpen = new OpenFileDialog())
                {
                    objOpen.Filter = strFilter;
                    if (objOpen.ShowDialog() == DialogResult.OK) strResult = objOpen.FileName;
                }
            }
            catch (Exception ex) { DevExpress.XtraEditors.XtraMessageBox.Show(ex.StackTrace, clsConst.gcstrAppName, MessageBoxButtons.OK, MessageBoxIcon.Error); }
            return strResult;
        }

        /// <summary>
        /// Kiem tra chuoi la null
        /// Author: QuyetLT
        /// </summary>
        /// <param name="strText">Chuoi dau vao</param>
        /// <param name="strMsg">Message thong bao</param>
        /// <returns>Bool</returns>
        public static bool fncIsBlank(string strText, string strMsg = null)
        {
            bool blnResult = false;
            try
            {
                if (DBNull.Value.Equals(strText)) return true;

                if (string.IsNullOrEmpty(strText))
                {
                    //show message and set focus if available
                    if (strMsg != null) fncMsgWarning(strMsg);
                    blnResult = true;
                }
            }
            catch (Exception ex) { throw ex; }
            return blnResult;
        }

        /// <summary>
        /// Convert gia tri sang DbDatetime
        /// </summary>
        /// <param name="dtValue"></param>
        /// <returns></returns>
        public static object fncCnvToDbDatetime(DateTime dtValue)
        {
            if (dtValue <= DateTime.MinValue || dtValue >= DateTime.MaxValue) return System.Data.SqlTypes.SqlDateTime.Null;
            return dtValue;
        }

        /// <summary>
        /// Convert gia tri sang DbString
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static object fncCnvToDbString(string strInput)
        {
            if (DBNull.Value.Equals(strInput)) return "";
            if (string.IsNullOrEmpty(strInput)) return "";

            //strInput = strInput.Replace("[", "[[]");
            //strInput = "'" + strInput.Replace("'", "''") + "'";
            //strInput = strInput.Replace("'", "''");

            return strInput;
        }

        /// <summary>
        /// Convert gia tri sang Dbbit
        /// Author: QuyetLT
        /// </summary>
        /// <param name="blnInput">Gia tri vao</param>
        /// <returns>1 hoac 0</returns>
        public static object fncCnvToDbBit(bool blnInput)
        {
            if (blnInput) return 1;
            return 0;
        }

        /// <summary>
        /// Convert gia tri sang DbImage
        /// </summary>
        /// <param name="objImage"></param>
        /// <returns></returns>
        public static object fncCnvToDbImage(Image objImage)
        {
            if (objImage == null) return "";
            return objImage;
        }

        /// <summary>
        /// Chuyen doi sang string
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>string</returns>
        public static string fncCnvNullToString(object objInput)
        {
            string strResult = "";

            try
            {
                //tra ve "" neu input la null
                if (Convert.IsDBNull(objInput)) return "";
                if (objInput == null) return "";
                strResult = Convert.ToString(objInput);
            }
            catch (Exception ex) { return ""; }

            return strResult;
        }

        /// <summary>
        /// Chuyen doi sang int
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>int</returns>
        public static int fncCnvNullToInt(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput);
            int intResult = 0;
            int.TryParse(strTemp, out intResult);
            return intResult;
        }

        /// <summary>
        /// Chuyen doi sang float
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>float</returns>
        public static float fncCnvNullToFloat(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput);
            float fltResult = 0F;
            float.TryParse(strTemp, out fltResult);
            return fltResult;
        }

        /// <summary>
        /// Chuyen doi sang bool
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>bool</returns>
        public static bool fncCnvNullToBool(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput).Trim();

            if (strTemp == "1") return true;
            if (strTemp == "0") return false;

            bool blnResult = false;
            bool.TryParse(strTemp, out blnResult);
            return blnResult;
        }

        /// <summary>
        /// Chuyen doi sang Datetime
        /// </summary>
        /// <param name="objInput"></param>
        /// <returns>DateTime</returns>
        public static DateTime fncCnvNullToDatetime(object objInput)
        {
            string strTemp = fncCnvNullToString(objInput);
            DateTime dtResult = DateTime.MinValue;
            DateTime.TryParse(strTemp, out dtResult);
            return dtResult;
        }

        /// <summary>
        /// Trả về HọĐệm + Tên từ họ tên đầy đủ
        /// Author: QuyetLT
        /// </summary>
        /// <param name="strHoten">Tên đầy đủ</param>
        /// <param name="strHoDem">Trả về họ đệm</param>
        /// <param name="strTen">Trả về tên</param>
        /// <returns>bool - true - thành công, false-thất bại</returns>
        public static bool fncGetHoTen(string strHoten, out string strHoDem, out string strTen)
        {
            strHoDem = "";
            strTen = "";

            try
            {
                strHoten = strHoten.Trim();

                int intIndex = strHoten.LastIndexOf(" ");

                if (intIndex <= 0)
                {
                    strHoDem = "";
                    strTen = strHoten;
                }
                else
                {
                    strHoDem = strHoten.Substring(0, intIndex + 1).Trim();
                    strTen = strHoten.Substring(intIndex).Trim();
                }

                return true;
            }
            catch (Exception ex)
            {
                clsCommon.fncMsgError(mcstrClassName, "fncGetData", ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Xây dựng câu query like
        /// Author: QuyetLT
        /// </summary>
        /// <param name="strField">Tên trường</param>
        /// <param name="strQuery">Dữ liệu query</param>
        /// <returns>string</returns>
        public static string fncBuildQueryLike(string strField, string strQuery)
        {
            string strReturnValue = null;

            strReturnValue = "";
            string[] strArr = null;

            try
            {
                //trim and remove double space
                strQuery = strQuery.Trim();
                strQuery = strQuery.Replace("  ", "");

                //split into array
                strArr = strQuery.Split(new char[] { ' ' });

                //start concat

                for (int i = 0; i <= strArr.Length - 2; i++)
                {
                    //build to get string like : FIELDNAME Like '%abc%'
                    strReturnValue += " " + strField;
                    strReturnValue += " LIKE ";
                    strReturnValue += "'";
                    strReturnValue += fncCnvToDbString("%" + strArr[i] + "%");
                    strReturnValue += "'";
                    strReturnValue += " AND";
                }

                //concat with the last item
                strReturnValue += " " + strField;
                strReturnValue += " LIKE ";
                strReturnValue += "'";
                strReturnValue += fncCnvToDbString("%" + strArr[strArr.Length - 1] + "%");
                strReturnValue += "'";
            }
            catch (Exception ex)
            {
                clsCommon.fncMsgError(mcstrClassName, "fncBuildQueryLike", ex.Message);
                return "";
            }
            finally
            {
                strArr = null;
            }
            return strReturnValue;
        }

        /// <summary>
        /// Release COM object
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool fncReleaseObject(object obj)
        {
            bool blnReturnValue = false;

            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;

                return true;
            }
            catch (Exception ex)
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
            return blnReturnValue;
        }
        
    }
}